package com.xiaocui.utils;


import com.xiaocui.domain.Point;

import java.util.ArrayList;
import java.util.List;

/**
 * @author vstarcui
 * @date 2018/7/25 0025
 */
public class GpsUtil {

    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    public static double getDistance(Point end, Point start) {
        double radLat1 = rad(end.getLat());
        double radLat2 = rad(start.getLat());
        double a = radLat1 - radLat2;
        double b = rad(end.getLng()) - rad(start.getLng());
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        s = s*1000;
        return s;
    }

    /**
     * 将百度坐标转为火星坐标
     *
     * @param point 转入的百度坐标
     * @return 转出的火星坐标
     */
    public static Point baiduPoint2GcjPoint(Point point) {
        return TransformPointer_mars2bd.bd2mars(point);
    }

    /**
     * 将火星坐标转为百度坐标
     *
     * @param point 转入的火星坐标
     * @return 转出的百度坐标
     */
    public static Point gcjPoint2BaiduPoint(Point point) {
        return TransformPointer_mars2bd.mars2bd(point);
    }

    /**
     * 将火星坐标转为84坐标
     *
     * @param point 转入的火星坐标
     * @return 转出的84坐标
     */
    public static Point gcjPoint2WgsPoint(Point point) {
        return TransformPointer_84tomars.GcjToWsgTransform(point);
    }

    /**
     * 将84坐标转为火星坐标
     *
     * @param point 转入的84坐标
     * @return 转出的火星坐标
     */
    public static Point wgsPoint2GcjPoint(Point point) {
        return TransformPointer_84tomars.WsgToGcjTransform(point);
    }

    /**
     * 将百度坐标转为84坐标
     *
     * @param point 转入的百度坐标
     * @return 转入的84坐标
     */
    public static Point baiduPoint2WgsPoint(Point point) {
        return gcjPoint2WgsPoint(baiduPoint2GcjPoint(point));
    }

    /**
     * 将84坐标转为百度坐标
     *
     * @param point 转入的84坐标
     * @return 转出的百度坐标
     */
    public static Point wgsPoint2BaiduPoint(Point point) {
        return gcjPoint2BaiduPoint(wgsPoint2GcjPoint(point));
    }

    /**
     * 批量将84坐标转为百度坐标
     * @param points 转入的坐标但数组
     * @return
     */
    public static List<Point> allWgsPoint2BaiduPoint(List<Point> points) {
        List<Point> pointList = new ArrayList<>();
        for (Point point : points) {
            pointList.add(wgsPoint2BaiduPoint(point));
        }
        return pointList;
    }
    /**
     * 批量将百度坐标转为84坐标
     * @param points
     * @return
     */
    public static List<Point> allBaiduPoint2WgsPoint(List<Point> points) {
        List<Point> pointList = new ArrayList<>();
        for (Point point : points) {
            pointList.add(baiduPoint2WgsPoint(point));
        }
        return pointList;
    }
}
